package com.zhijiaoyun.controller;

import com.zhijiaoyun.common.CommonUtils;
import com.zhijiaoyun.common.DsConstant;
import com.zhijiaoyun.common.ValidationUtils;
import com.zhijiaoyun.controller.base.BaseController;
import com.zhijiaoyun.model.Admin;
import com.zhijiaoyun.model.Authority;
import com.zhijiaoyun.service.IAdminService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

/**
 * @author shisfish
 * @date 2017/6/6
 */
@Controller
@RequestMapping(value = "main", produces = "application/json; charset=utf-8")
public class LoginController extends BaseController {

    @Resource
    IAdminService adminService;

    @ResponseBody
    @RequestMapping(value = "/login", method = RequestMethod.POST)
    public Map<String, Object> login(String account, String password) {
        Admin admin = adminService.getAdminByAccount(account);
        if (admin == null) {
            return responseLoginError();
        }
        if (!admin.getPassword().equals(CommonUtils.MD5(password))) {
            return responseLoginError();
        }

        Map<String, Object> map = new HashMap<>();

        String token = ValidationUtils.createJWT(admin, ValidationUtils.DAY_ONE);
        map.put("token", token);

        Authority authority = authorityService.getAuthorityByAuthorityId(admin.getAuthorityId());
        if (authority != null) {
            map.put("authority", authority.getModuleIds());
        }
        admin.setPassword("");
        map.put("admin", admin);

        return responseSuccessMap(map);
    }

    @ResponseBody
    @RequestMapping(value = "/changePassword", method = RequestMethod.POST)
    public Map<String, Object> changePassword(String newPassword, String password, String token) {
        Map<String, Object> map = new HashMap<>();
        Long adminId = CommonUtils.getAdminIdByToken(token);
        if (adminId == 0) {
            map.put(DsConstant.MESSAGE, "token过期");
            return map;
        }
        Admin admin = adminService.getById(adminId);
        if (admin == null) {
            return responseAdminLost();
        }

        if (!admin.getPassword().equals(CommonUtils.MD5(password))) {
            return responseErrorMap(DsConstant.CODE_OLD_PASSWORD_ERROR, DsConstant.MESSAGE_OLD_PASSWORD_ERROR);
        }

        admin.setPassword(CommonUtils.MD5(newPassword));
        adminService.update(admin);
        return responseSuccessMap();
    }

    @ResponseBody
    @RequestMapping(value = "/index", method = RequestMethod.GET)
    public Map<String, Object> index() {
        Map<String, Object> map = new HashMap<>();

        // 用户数
        int userNum = userService.countAll();
        map.put("userNum", userNum);

        // 课程数
        int courseNum = courseService.countAll();
        map.put("courseNum", courseNum);

        // 课程数
        int lessonNum = lessonService.countAll();
        map.put("lessonNum", lessonNum);

        // 资讯数
        int newsNum = newsService.countAll();
        map.put("newsNum", newsNum);

        return responseSuccessMap(map);
    }

}
